//
//  RollingNoticeView.m
//  glt
//
//  Created by RubyJobs on 2018/4/23.
//  Copyright © 2018年 LiuXingBang. All rights reserved.
//

#import "RollingNoticeView.h"
#import "UIView+CustomSize.h"
@interface RollingNoticeView()<UIScrollViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *middleImageView;
@property (nonatomic, strong) UIImageView *rightImageView;

@property (nonatomic, assign) NSInteger curIndex;

/// scroll timer
@property (nonatomic, strong) NSTimer *rotateTimer;

/// scroll duration
@property (nonatomic, assign) NSTimeInterval scrollDuration;
@end
@implementation RollingNoticeView

- (instancetype)init{
    self = [super init];
    if (self) {
        self.scrollDuration = 2;
        self.pagingEnabled = YES;
        self.delegate = self;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        
        
        [self addSubview:self.leftImageView];
        [self addSubview:self.middleImageView];
        [self addSubview:self.rightImageView];
       
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollDuration = 2;
        //设置分页效果
        self.pagingEnabled = YES;
        self.delegate = self;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        [self addSubview:self.leftImageView];
        [self addSubview:self.middleImageView];
        [self addSubview:self.rightImageView];
        [self setScrollViewContentOffsetCenter];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
     self.leftImageView.frame = CGRectMake(0, self.custom_height *0, self.custom_width, self.custom_height);
    self.middleImageView.frame = CGRectMake(0, self.custom_height *1, self.custom_width, self.custom_height);
    self.rightImageView.frame = CGRectMake(0, self.custom_height *2, self.custom_width, self.custom_height);
    self.contentSize = CGSizeMake(0, self.custom_height * 3);
}

- (void)setScrollViewContentOffsetCenter {
    self.contentOffset = CGPointMake(0, self.custom_height);
}


- (void)timerHandle
{
    [self setContentOffset:CGPointMake(0,self.contentOffset.y + self.custom_height) animated:YES];
    
}


- (void)setBannerList:(NSMutableArray *)bannerList{
    _bannerList = bannerList;
    _bannerList = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg"].mutableCopy;
    self.rotateTimer = [NSTimer scheduledTimerWithTimeInterval:self.scrollDuration target:self selector:@selector(timerHandle) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.rotateTimer forMode:NSRunLoopCommonModes];

}
- (void)setCurIndex:(NSInteger)curIndex {
    if (_curIndex >= 0) {
        _curIndex = curIndex;
        
        // caculate index
        NSInteger imageCount = _bannerList.count;
        NSInteger leftIndex = (curIndex + imageCount - 1) % imageCount;
        NSInteger rightIndex= (curIndex + 1) % imageCount;
        
        // TODO: if need use image from server, can import SDWebImage SDK and modify the codes below.
        // fill image
        self.leftImageView.image = [UIImage imageNamed:_bannerList[leftIndex]];
        self.middleImageView.image = [UIImage imageNamed:_bannerList[curIndex]];
        self.rightImageView.image = [UIImage imageNamed:_bannerList[rightIndex]];
        
        // every scrolled, move current page to center
        [self setScrollViewContentOffsetCenter];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [self caculateCurIndex];
}
- (void)caculateCurIndex {
    if (_bannerList.count > 0) {
        CGFloat pointY = self.contentOffset.y;
        
        // judge critical value，first and third imageView's contentoffset
        CGFloat criticalValue = .2f;
        
        // scroll right, judge right critical value
        if (pointY > 2 * self.custom_height - criticalValue) {
            self.curIndex = (self.curIndex + 1) % _bannerList.count;
        } else if (pointY < criticalValue) {
            // scroll left，judge left critical value
            self.curIndex = (self.curIndex + _bannerList.count - 1) %_bannerList.count;
        }
    }
}
- (void)scrollTimerDidFired:(NSTimer *)timer {
    // correct the imageview's frame, because after every auto scroll,
    // may show two images in one page
    
    CGFloat criticalValue = .2f;
    if (self.contentOffset.y < self.custom_height - criticalValue || self.contentOffset.y > self.custom_height + criticalValue) {
        [self setScrollViewContentOffsetCenter];
    }
    CGPoint newOffset = CGPointMake(self.contentOffset.x, self.contentOffset.y + self.custom_height);
    [self setContentOffset:newOffset animated:YES];
}

- (UIImageView *)leftImageView {
    if (!_leftImageView) {
        _leftImageView = [UIImageView new];
        _leftImageView.contentMode = UIViewContentModeScaleAspectFit;
        //_leftImageView.backgroundColor = [UIColor yellowColor];
    }
    
    return _leftImageView;
}

- (UIImageView *)middleImageView {
    if (!_middleImageView) {
        _middleImageView = [UIImageView new];
        _middleImageView.contentMode = UIViewContentModeScaleAspectFit;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked:)];
        [_middleImageView addGestureRecognizer:tap];
        //_middleImageView.backgroundColor = [UIColor redColor];
        _middleImageView.userInteractionEnabled = YES;
    }
    
    return _middleImageView;
}

- (UIImageView *)rightImageView {
    if (!_rightImageView) {
        _rightImageView = [UIImageView new];
        _rightImageView.contentMode = UIViewContentModeScaleAspectFit;
        //_rightImageView.backgroundColor = [UIColor greenColor];
    }
    
    return _rightImageView;
}
@end
